-- UIConfirmTip
-- Create by huanghr Apr/22/2016
-- 显示确认提示

-- 每行最多的文字数量
local LINE_WORD_COUNT = 15;

-- bg编号
local SMALL_BG_COUNT = 4;

--UIConfirmTip继承自Layer
UIConfirmTip = class("UIConfirmTip", function()
    return cc.Layer:create();
end)

----------------------------------------------------
-- @param title        确定标题文本
-- @param msg          提示框的内容
function UIConfirmTip.create(title, msg)
    return UIConfirmTip.new(title, msg);
end

-- 内部函数声明
local resize;

-- 构造函数
function UIConfirmTip:ctor(title, msg)
    self:setName("UIConfirmTip");
    -- 初始化
    local node = cc.CSLoader:createNode("layout/dialog/ConfirmTip.csb");
    self:addChild(node);
    self.node = node;

    local CT = findChildByName(node, "CT");

    self.title = title;
    self.msg = msg;

    -- 重绘
    self:redraw();

    -- 注册点击事件
    self:registerTouchEvent();

    -- 调整高度
    self:adjustHeight();

    -- 适配
    resize(node);
end

-- 重绘
function UIConfirmTip:redraw()
    local node = self.node;
    local CT = findChildByName(node, "CT");
    local title = self.title;
    local msg = self.msg;

    -- 设置标题
    self.titleLabel = CT:getChildByName("title");

    TextStyleM.setTitleStyle(self.titleLabel);
    self.titleLabel:setString(title);

    -- 设置内容
    local contentLabel = CT:getChildByName("content");
    TextStyleM.setTextStyle(contentLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
    contentLabel:getVirtualRenderer():setLineHeight(TextStyleM.TEXT_SIZE_SMALL + 5);

    contentLabel = applyString(contentLabel, msg, nil, nil, nil, LINE_WORD_COUNT);

    self.contentLabel = contentLabel;
end

-- 注册点击事件
function UIConfirmTip:registerTouchEvent()
    -- 注册背景点击事件
    local bgImg = findChildByName(self.node, "bg/bgImg");
    local function onBgClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            AudioM.playFx("button_return");
            -- 关闭当前界面
            UIMgr.getCurrentScene():removeDialog(self);
        end
    end
    bgImg:addTouchEventListener(onBgClick);

    -- 防止点击事件被吞掉
    local bg = findChildByName(self.node, "CT/bg_frame");
    for i = 1, SMALL_BG_COUNT do
        local subBg = findChildByName(bg, "bg" .. i);
        if subBg ~= nil then
            subBg:setSwallowTouches(false);
        end
    end
end

-- 调整高度
function UIConfirmTip:adjustHeight()
    -- 老的高度和缩放
    local bgFrame = findChildByName(self.node, "CT/bg_frame");
    local oldHeight = 272 * bgFrame:getScaleY();
    local oldScaleY = bgFrame:getScaleY();

    local textHeight = self.contentLabel:getContentSize().height * 1.2;

    local margin = 160;
    local newHeight = textHeight + margin * 2 - 100;

    -- 新的Y轴缩放
    local newScaleY = newHeight * oldScaleY / oldHeight;
    bgFrame:setScaleY(newScaleY);

    local delta = (newHeight - oldHeight) / 2;

    -- 标题上移delta像素
    self.titleLabel:setPositionY(self.titleLabel:getPositionY() + delta);

    self.contentLabel:setPositionY(self.contentLabel:getPositionY() + delta);

    -- 底图
    local bg1 = findChildByName(self.node, "CT/bg1");
    local bg2 = findChildByName(self.node, "CT/bg2");
    bg1:setPositionY(bg1:getPositionY() + delta);
    bg2:setPositionY(bg2:getPositionY() + delta);
end

-- 适配
resize = function(node)
    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");
end

return UIConfirmTip;
